package com.example.producer;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class EmitLog {

    private static final String EXCHANGE_NAME = "logs";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername("admin");
        factory.setPassword("123456");
        factory.setHost("localhost");
        try (Connection connection = factory.newConnection();
            Channel channel = connection.createChannel()) {

            // 声明一个名为logs，类型为fanout的交换机
            //AMQP.Exchange.DeclareOk ok1 = channel.exchangeDeclare(EXCHANGE_NAME, "direct");
            channel.exchangeDelete(EXCHANGE_NAME);
            AMQP.Exchange.DeclareOk ok2 =channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
            //channel.exchangeDeclare("exc2", "direct");

            String message = argv.length < 1 ? "info: Hello World!0000888" :
                    String.join(" ", argv);

            // 将消息发布到logs交换机
            channel.basicPublish(EXCHANGE_NAME, "info", null, message.getBytes("UTF-8"));
            //channel.basicPublish("exc2", "info", null, message.getBytes("UTF-8"));
            System.out.println(" [x] Sent '" + message + "'");
        }
    }
}
